package cn.rengy.web.framework.support;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

import cn.rengy.lang.DataConverter;

@Component
public class DataConverterImpl implements DataConverter {

	@Autowired
	private ObjectMapper objectMapper;
	
	private XmlMapper xmlMapper=new XmlMapper();
	
	
	@Override
	public String beanToXml(Object bean) throws JsonProcessingException{
		return xmlMapper.writeValueAsString(bean);
	}


	@Override
	public String beanToJson(Object bean) throws JsonProcessingException {
		return objectMapper.writeValueAsString(bean);
	}

	@Override
	public <T> T jsonToBean(String jsonStr, Class<T> valueType) throws JsonMappingException, JsonProcessingException {
		return objectMapper.readValue(jsonStr, valueType);
	}




	@Override
	public <T> T xmlToBean(String xml, Class<T> valueType) throws JsonMappingException, JsonProcessingException {
		return xmlMapper.readValue(xml, valueType);
	}
	
	@Override
	public <T> T xmlToBean(InputStream inputStream, Class<T> valueType) throws JsonParseException, JsonMappingException, IOException {
		return xmlMapper.readValue(inputStream, valueType);
	}

	@Override
	public Map<String, Object> xmlToMap(String xml) throws Exception {
		
		return null;
	}

}
